package com.youlai.system.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.system.converter.FrequencyConverter;
import com.youlai.system.mapper.SysFrequencyMapper;
import com.youlai.system.model.entity.SysFrequency;
import com.youlai.system.model.form.FrequencyForm;
import com.youlai.system.model.query.FrequencyPageQuery;
import com.youlai.system.model.vo.FrequencyPageVO;
import com.youlai.system.service.SysFrequencyService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 评估频次业务接口实现
 */
@Service
@RequiredArgsConstructor
public class SysFrequencyServiceImpl extends ServiceImpl<SysFrequencyMapper, SysFrequency> implements SysFrequencyService {

    private final FrequencyConverter frequencyConverter;

    @Override
    public Page<FrequencyPageVO> getFrequencyPage(FrequencyPageQuery queryParams) {
        // 参数构建
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        Page<FrequencyPageVO> page = new Page<>(pageNum, pageSize);
        // 查询数据
        Page<FrequencyPageVO> frequencyPageVOPage = this.baseMapper.getFrequencyPage(page, queryParams);
        // 实体转换
        return frequencyPageVOPage;
    }

    @Override
    public boolean saveFrequency(FrequencyForm frequencyForm) {
        // 实体转换
        SysFrequency frequency = frequencyConverter.form2Entity(frequencyForm);
        LambdaQueryWrapper<SysFrequency> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysFrequency::getGradeId, frequency.getGradeId());
//        queryWrapper.eq(SysFrequency::getMoldId, frequency.getMoldId());
        queryWrapper.eq(SysFrequency::getCourseId, frequency.getCourseId());
        long count = this.count(queryWrapper);
        Assert.isTrue(count == 0, "职级故障评估频次已存在");
        return save(frequency);
    }

    @Override
    public boolean updateFrequency(Long frequencyId, FrequencyForm frequencyForm) {
        // form -> entity
        SysFrequency frequency = frequencyConverter.form2Entity(frequencyForm);
        LambdaQueryWrapper<SysFrequency> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysFrequency::getGradeId, frequency.getGradeId());
//        queryWrapper.eq(SysFrequency::getMoldId, frequency.getMoldId());
        queryWrapper.eq(SysFrequency::getCourseId, frequency.getCourseId());
        queryWrapper.ne(SysFrequency::getId, frequency.getId());
        long count = this.count(queryWrapper);
        Assert.isTrue(count == 0, "职级故障评估频次已存在");
        // 修改评估频次
        return this.updateById(frequency);
    }

    @Override
    public FrequencyForm getFrequencyForm(Long frequencyId) {
        SysFrequency entity = this.getById(frequencyId);
        return frequencyConverter.entity2Form(entity);
    }

    @Override
    public boolean deleteFrequencies(String idsStr) {
        Assert.isTrue(StrUtil.isNotBlank(idsStr), "评估频次删除数据为空");
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(ids);
    }

    @Override
    public boolean deleteFrequencies(List<Long> idList) {
        return this.removeByIds(idList);
    }

    @Override
    public List<Long> getCourseIdListByGradeId(Long gradeId) {
        LambdaQueryWrapper<SysFrequency> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysFrequency::getGradeId, gradeId);
        queryWrapper.select(SysFrequency::getCourseId);
        return list(queryWrapper).stream().map(SysFrequency::getCourseId).collect(Collectors.toList());
    }

    @Override
    public SysFrequency getOneByGradeIdAndCourseId(Long gradeId, Long courseId) {
        LambdaQueryWrapper<SysFrequency> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysFrequency::getGradeId, gradeId);
        queryWrapper.eq(SysFrequency::getCourseId, courseId);
        return getOne(queryWrapper);
    }
}
